package com.asa.fushu.matrix;

import com.asa.fushu.bean.Fushu;
import com.asa.hutils.MathHUtils;
import com.asa.hutils.Ni;



/**
 * 计算复数矩阵的逆
 * 公式查看图片： 复数矩阵的逆.png
 * 它好像是要求复矩阵的实部和虚部的矩阵都要可逆
 * 
 * @author Administrator
 *
 */
public class Ni_FUSHU {

	/**
	 * 这种方式要求实部和虚部的矩阵都是可逆的，它们之中某一个不可逆是否能求出解我就不得而知了
	 * @param a
	 * @return
	 * @throws Exception
	 */
	public static Fushu[][] ni(Fushu[][] a) throws Exception{
		
		double[][] reals = new double[a.length][a.length];
		double[][] images = new double[a.length][a.length];

		
		for (int i = 0; i < a.length; i++) {
			for (int j = 0; j < a[i].length; j++) {
				reals[i][j] = a[i][j].getReal();
				images[i][j] = a[i][j].getIm();
			}
		}
		
		double[][] juzhengni_reals = MathHUtils.juzhengni(reals);
//		double[][] juzhengni_images = MathHUtils.juzhengni(images);

		
//		double[][] result_reals = new double[a.length][a.length];
//		double[][] result_images= new double[a.length][a.length];

//		reals
//		MathHUtils.chenfa(MathHUtils.chenfa(images, juzhengni_reals),images);
		
		double[][] A_BAB = MathHUtils.jiafa(reals, MathHUtils.chenfa(MathHUtils.chenfa(images, juzhengni_reals),images));
		double[][] result_reals = MathHUtils.juzhengni(A_BAB);
		
		double[][] result_images = MathHUtils.chenfa(MathHUtils.chenfa(juzhengni_reals, images),result_reals);
		System.out.println();
//		for (int i = 0; i < result_reals.length; i++) {
//			for (int j = 0; j < result_reals[i].length; j++) {
//				System.out.print(result_reals[i][j]+"       ");
//			}
//			System.out.println();
//		}
//		
//		System.out.println("images________________");
//		for (int i = 0; i < result_images.length; i++) {
//			for (int j = 0; j < result_images[i].length; j++) {
//				System.out.print(result_images[i][j]+"       ");
//			}
//			System.out.println();
//		}
		
		Fushu[][] result = new Fushu[a.length][a.length];
		
		for (int i = 0; i < result.length; i++) {
			result[i] = new Fushu[a.length];
			for (int j = 0; j < result[i].length; j++) {
				result[i][j] = new Fushu(result_reals[i][j],result_images[i][j]);
			}
			
		}
		
		
		
		return result;

	}
	
	
	
	
	


	
	//求复数矩阵的逆
	public static void main(String[] args) throws Exception {
		
		Fushu[][] a = new Fushu[4][4];
		a[0][0] = new Fushu(4, 2);
		a[0][1] = new Fushu(3, 1);
		a[0][2] = new Fushu(4, 3);
		a[0][3] = new Fushu(5, 5);

		a[1][0] = new Fushu(1, 7);
		a[1][1] = new Fushu(8, 2);
		a[1][2] = new Fushu(2, 2);
		a[1][3] = new Fushu(9, 3);
		
		a[2][0] = new Fushu(4, 4);
		a[2][1] = new Fushu(5, 6);
		a[2][2] = new Fushu(1, 7);
		a[2][3] = new Fushu(7, 2);
		
		a[3][0] = new Fushu(6, 1);
		a[3][1] = new Fushu(7, 8);
		a[3][2] = new Fushu(1, 4);
		a[3][3] = new Fushu(2, 5);
		
		
		
		
//		double[][] asa = new double[a[0].length][a.length];
//
//		for (int i = 0; i < asa.length; i++) {
//			for (int j = 0; j < asa[i].length; j++) {
//				System.out.println(asa[i][j]);
//			}
//		}
		
		
		Fushu[][] ni = ni(a);
		
		for (int i = 0; i < ni.length; i++) {
			for (int j = 0; j < ni.length; j++) {
				ni[i][j].shownumber();
				System.out.print("        ");
			}
			System.out.println();
		}
		
		
		
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
}
